#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using Gizmox.WebGUI.Common.Resources;
using TVC.Layout;
using System.Xml;
using System.IO;

#endregion

namespace TVC.Modules.Ref
{
    public partial class Vendor : ModuleParent, IHostedApplication
    {
        int idAllowEditLanguage = 1;
        int idAddNew = 206;
        int idEdit = 207;
        int idDelete = 208;
        int idTransferIn = 209;
        int idTransferOut = 209;
        int idToExcel = 275;

        bool _isCalculate = true;
        string strAction = "";

        public Vendor()
        {
            InitializeComponent();
            _dgvList = dgvList;
            _strType = "Vendor";
        }

        public void InitializeApplication()
        {
        }


        public void SetObject(object obj)
        {
        }

        public HostedToolBarElement[] GetToolBarElements()
        {
            ArrayList objElements = new ArrayList();
            if (IsPermis(idAddNew)) objElements.Add(new HostedToolBarButton("Add New", "New", new IconResourceHandle("Add.png"), "C"));
            if (IsPermis(idAddNew) && IsPermis(idEdit)) objElements.Add(new HostedToolBarButton("Save", "Save", new IconResourceHandle("Save.png"), "S"));
            if (IsPermis(idAddNew)) objElements.Add(new HostedToolBarButton("Copy", "Copy", new IconResourceHandle("Copy.png"), "O"));
            if (IsPermis(idEdit)) objElements.Add(new HostedToolBarButton("Edit", "Edit", new IconResourceHandle("Edit.png"), "A"));
            objElements.Add(new HostedToolBarButton("Skip", "Skip", new IconResourceHandle("Reject.png"), "K"));
            if (IsPermis(idDelete)) objElements.Add(new HostedToolBarButton("Delete", "Delete", new IconResourceHandle("Delete.png"), "X"));
            if (IsPermis(idAddNew)) objElements.Add(new HostedToolBarButton("Sun", "Insert To SunSystems", new IconResourceHandle("Add.png"), "TS"));

            objElements.Add(new HostedToolBarSeperator());
            if (IsPermis(idTransferIn)) objElements.Add(new HostedToolBarButton("Transfer In", "Import", new IconResourceHandle("Down.png"), "TI"));
            if (IsPermis(idTransferOut)) objElements.Add(new HostedToolBarButton("To Xml", "To Xml", new IconResourceHandle("Up.png"), "TO"));
            if (IsPermis(idToExcel)) objElements.Add(new HostedToolBarButton("To Excel", "To Excel", new IconResourceHandle("Excel.png"), "P"));
            //objElements.Add(new HostedToolBarSeperator());
            //if (IsPermis(idPrint))
            //    objElements.Add(new HostedToolBarButton("Print", new IconResourceHandle("Print.png"), "P"));
            objElements.Add(new HostedToolBarButton("Refresh", "Refresh", new IconResourceHandle("Refresh.png"), "R", false));

            return (HostedToolBarElement[])objElements.ToArray(typeof(HostedToolBarElement));
        }

        protected override void SetupToolBar(string strAction)
        {
            ToolBar tbarLocal = GetTbarLocal();
            bool on = true, off = false;
            if (tbarLocal == null) return;

            switch (strAction)
            {
                case "C":
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TI") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TO") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "P") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "R") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "K") { button.Enabled = on; break; } }
                    break;
                case "A":
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TI") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TO") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "P") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "R") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "K") { button.Enabled = on; break; } }
                    break;
                case "S":
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TI") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TO") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "P") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "R") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "K") { button.Enabled = on; break; } }
                    break;

                case "O":
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TI") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TO") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "P") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "R") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "K") { button.Enabled = on; break; } }
                    break;
                case "V":
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TI") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TO") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "P") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "R") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "K") { button.Enabled = on; break; } }
                    break;
            }

        }
        public void OnToolBarButtonClick(HostedToolBarButton objButton, EventArgs objEvent)
        {
            try
            {
                sErr = "";
                strAction = (string)objButton.Tag;

                BUS.VENDORControl ctr = new BUS.VENDORControl();
                switch (strAction)
                {
                    case "C": AddNew(txtVendorCode);
                        break;

                    case "A": editRecord(txtVendorCode);
                        break;

                    case "S": saveRecord(txtVendorCode);

                        break;
                    case "O": copy(txtVendorCode);
                        break;

                    case "X": delete(txtVendorCode);

                        break;
                    case "K": skip();

                        break;
                    case "TI":
                        Forms.frmTransferIn frmTI = new Forms.frmTransferIn(_strType, GetMainForm());
                        frmTI.ShowDialog();
                        break;
                    case "TO":
                        export2Xml();
                        break;
                    case "R":
                        reloadList();
                        break;
                    case "TS":
                        InsertToSun();
                        break;
                    case "P":
                        export2Excel();
                        break;
                }

                if (sErr == "") ThrowMsg("Command: " + _processStatus);
            }
            catch (Exception ex)
            {
                ThrowMsg(ex.Message + Environment.NewLine + ex.Source);
            }
        }


        private void Vendor_Load(object sender, EventArgs e)
        {
            refreshForm("");
            enableForm(false);
        }


        #region .::| Override Functions |::.
        protected override ErrorStatus validateForm(ref string sErr)
        {
            ErrorStatus result = ErrorStatus.SUCCESS;
            BUS.VENDORControl ctr = new BUS.VENDORControl();
            sErr = "";
            errProvider.SetError(txtVendorCode, "");
            errProvider.SetError(txtVendorName, "");

            if (txtVendorCode.Text == "")
            {
                sErr += "Coud not save blank Vendor Code, Please check!";
                errProvider.SetError(txtVendorCode, "Coud not save blank Vendor Code, Please check!");
                txtVendorCode.Focus();
            }

            if (txtVendorName.Text == "")
            {
                sErr += "Coud not save blank Vendor Name, Please check!";
                errProvider.SetError(txtVendorName, "Coud not save blank Vendor Name, Please check!");
                txtVendorName.Focus();
            }


            if (sErr != "") result = ErrorStatus.ERROR;

            return result;
        }
        protected override bool checkExists()
        {
            BUS.VENDORControl ctr = new BUS.VENDORControl();
            return ctr.IsExist(GetMainForm().PODInfo.Business_ID, txtVendorCode.Text);
        }

        protected override ErrorStatus beforeAdd(ref string sErr)
        {
            ErrorStatus result = ErrorStatus.SUCCESS;
            BUS.VENDORControl ctr = new BUS.VENDORControl();
            if (ctr.IsExist_Name(GetMainForm().PODInfo.Business_ID, txtVendorName.Text))
            {
                result = ErrorStatus.ERROR;
                sErr = txtVendorName.Text + " is already existed, please check!";
            }
            return result;
        }

        protected override string addRecord(bool isDraft = false)
        {
            string sErr = "";
            BUS.VENDORControl ctr = new BUS.VENDORControl();
            DTO.VENDORInfo info = GetData();
            ctr.Add(GetMainForm().PODInfo.Business_ID, info, ref sErr);

            return sErr;
        }
        protected override string updateRecord(bool isDraft = false)
        {
            string sErr = "";
            BUS.VENDORControl ctr = new BUS.VENDORControl();
            DTO.VENDORInfo info = GetData();
            sErr = ctr.Update(GetMainForm().PODInfo.Business_ID, info);

            return sErr;
        }
        protected override string delRecord()
        {
            BUS.VENDORControl ctr = new BUS.VENDORControl();

            return ctr.Delete(GetMainForm().PODInfo.Business_ID, txtVendorCode.Text);

        }

        protected override void refreshForm(string str)
        {
            _isCalculate = false;
            txtVendorCode.Text = "";
            txtVendorName.Text = "";
            txtBeneficiary.Text = "";
            txtBankName.Text = "";
            txtAccountNo1.Text = "";
            txtAccountNo2.Text = "";
            txtSwiftCode.Text = "";
            txtVATCode.Text = "";
            txtAddress.Text = "";
            txtFax_Tel.Text = "";
            txtEmail.Text = "";
            txtContract.Text = "";
            txtNote1.Text = "";
            txtNote2.Text = "";
            _isCalculate = true;
        }
        protected override void enableForm(bool val)
        {
            //gbPrimary.SuspendLayout();
            txtVendorCode.Enabled = val;
            txtVendorName.Enabled = val;
            txtBeneficiary.Enabled = val;
            txtBankName.Enabled = val;
            txtAccountNo1.Enabled = val;
            txtAccountNo2.Enabled = val;
            txtSwiftCode.Enabled = val;
            txtVATCode.Enabled = val;
            txtAddress.Enabled = val;
            txtFax_Tel.Enabled = val;
            txtEmail.Enabled = val;
            txtContract.Enabled = val;
            txtNote1.Enabled = val;
            txtNote2.Enabled = val;

            if (val == false)
            {
                foreach (Control x in panel1.Controls)
                {
                    if (errProvider.GetError(x) != string.Empty) errProvider.SetError(x, "");
                }
            }
            // gbPrimary.ResumeLayout(true);
        }

        protected override void loadRecord(object o)
        {
            if (o is DataRowView)
            {
                DTO.VENDORInfo inf = new DTO.VENDORInfo((DataRowView)o);
                SetData(inf);
            }
        }

        protected override void reloadList()
        {
            GetMainForm().LoadDataTable(_strType);
            bisVendor.DataSource = GetMainForm().GetTable(_strType);
        }
        #endregion



        private void SetData(DTO.VENDORInfo info)
        {
            txtVendorCode.Text = info.VendorCode;
            txtVendorName.Text = info.VendorName;
            txtBeneficiary.Text = info.Beneficiary;
            txtBankName.Text = info.BankName;
            txtAccountNo1.Text = info.AccountNo1;
            txtAccountNo2.Text = info.AccountNo2;
            txtSwiftCode.Text = info.SwiftCode;
            txtVATCode.Text = info.VATCode;
            txtAddress.Text = info.Address;
            txtFax_Tel.Text = info.Fax_Tel;
            txtEmail.Text = info.Email;
            txtContract.Text = info.ContactPerson;
            txtNote1.Text = info.Note1;
            txtNote2.Text = info.Note2;
        }

        private DTO.VENDORInfo GetData()
        {
            return new DTO.VENDORInfo(
                txtVendorCode.Text,
                txtVendorName.Text,
                txtBeneficiary.Text,
                txtBankName.Text,
                txtAccountNo1.Text,
                txtAccountNo2.Text,
                txtSwiftCode.Text,
                txtVATCode.Text,
                txtAddress.Text,
                txtFax_Tel.Text,
                txtEmail.Text,
                txtContract.Text,
                txtNote1.Text,
                txtNote2.Text,
                0, ""
                );

        }


        private void InsertToSun()
        {
            string db = GetMainForm().SUNDB;
            string connect = MainForm.HLConnect;

            BUS.SSRFANVControl ctr = new BUS.SSRFANVControl();
            DTO.SSRFANVInfo info = GetSSRFANV();
            info.SUN_DB = db;
            if (ctr.IsExist(info.SUN_DB, info.CATEGORY, info.CODE, connect))
            {
                MessageBox.Show("This code is already exists in SunSystems.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning, false);
                return;
            }

            if (txtVendorName.Text.Length > 30)
            {
                MessageBox.Show("Vendor name is more than 30 characters, i will be truncated.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning, false);
            }

            ctr.Add(info, ref sErr, connect);

            if (sErr != "")
            {
                MessageBox.Show(sErr, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, false);
            }
            else
                MessageBox.Show("This code has been created successfully in SunSystems!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information, false);

        }

        private DTO.SSRFANVInfo GetSSRFANV()
        {
            return new DTO.SSRFANVInfo(
                GetMainForm().SUNDB,
                "T6",
                txtVendorCode.Text,
                "",
                DateTime.Now.ToString("yyyyMMdd"),
                txtVendorName.Text,
                "",
                "",
                "",
                ""
                );

        }


    }
}